package com.live.dengdengtv.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.ChatMessage;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.utils.DateUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.TIMElem;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wyf on 2016/9/21.
 */
public class MsgAdapter extends BaseAdapter {

    private List<ChatMessage> chatFriendList = LoginInfo.getInstance().getChatFriendList();
    private List<ChatMessage> chatStrangerList = LoginInfo.getInstance().getChatStrangerList();

    private List<ChatMessage> listViewData;

    private Context context;
    public MsgAdapter(int type, Context context){
        if(type == 1){
            listViewData = chatFriendList;
        }else if(type == 2){
            listViewData = chatStrangerList;
        }
        this.context = context;
    }
    @Override
    public int getCount() {
        return listViewData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView != null) {
            holder = (ViewHolder)convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.card_people_info, null);

            holder = new ViewHolder();
            holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            holder.msg_tv = (TextView) convertView.findViewById(R.id.msg_tv);
            holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
            holder.head_iv = (ImageView) convertView.findViewById(R.id.head_iv);
            holder.sex_iv = (ImageView) convertView.findViewById(R.id.sex_iv);
            holder.level_iv = (ImageView) convertView.findViewById(R.id.level_iv);

            convertView.setTag(holder);
        }

        holder.name_tv.setText(listViewData.get(position).getNickName());
        holder.msg_tv.setText(listViewData.get(position).getMsg());
        long time = listViewData.get(position).getTime();
        String date = DateUtils.dateToString(new Date(time),"yyyy年M月d日");
        holder.time_tv.setText(date);
        ImageLoader.getInstance().displayImage(listViewData.get(position).getFaceUrl(), holder.head_iv);
        return convertView;
    }

    private static class ViewHolder{
        TextView name_tv;
        TextView msg_tv;
        TextView time_tv;
        ImageView head_iv;
        ImageView sex_iv;
        ImageView level_iv;
    }
}
